package org.elanfox.cloud.common.base;

import lombok.Data;
import org.elanfox.cloud.common.enums.IResultCode;
import org.elanfox.cloud.common.enums.ResultTypeEnum;

import java.beans.Transient;
import java.io.Serializable;

/**
 * @author allan
 * @date 2019-08-16
 */
@Data
public class ApiResult<T> implements Serializable {
    private static final long serialVersionUID = -6109881088423213930L;
    /**
     * 结果类型
     * {@link org.elanfox.cloud.common.enums.ResultTypeEnum}
     */
    private int type;
    /**
     * 错误码
     */
    private String code;
    /**
     * 信息
     */
    private String msg;
    /**
     * 封装的数据类型
     */
    private T data;


    public static <T> ApiResult<T> success(T data) {
        ApiResult<T> apiResult = new ApiResult<>();
        apiResult.type = ResultTypeEnum.SUCCESS.getCode();
        apiResult.msg = ResultTypeEnum.SUCCESS.getDesc();
        apiResult.data = data;

        return apiResult;
    }

    public static <T> ApiResult<T> success() {
        ApiResult<T> apiResult = new ApiResult<>();
        apiResult.type = ResultTypeEnum.SUCCESS.getCode();
        apiResult.msg = ResultTypeEnum.SUCCESS.getDesc();

        return apiResult;
    }

    public static <T> ApiResult<T> exception(IResultCode resultCode) {
        ApiResult<T> apiResult = new ApiResult<>();
        apiResult.type = ResultTypeEnum.EXCEPTION.getCode();
        apiResult.setCode(resultCode.getCode());
        apiResult.setMsg(resultCode.getMsg());

        return apiResult;
    }

    public static <T> ApiResult<T> exception(String code, String msg) {
        ApiResult<T> apiResult = new ApiResult<>();
        apiResult.type = ResultTypeEnum.EXCEPTION.getCode();
        apiResult.setCode(code);
        apiResult.setMsg(msg);

        return apiResult;
    }

    public static <T> ApiResult<T> exception(IResultCode resultCode, String msg) {
        ApiResult<T> apiResult = new ApiResult<>();
        apiResult.type = ResultTypeEnum.EXCEPTION.getCode();
        apiResult.setCode(resultCode.getCode());
        apiResult.setMsg(msg);

        return apiResult;
    }

    public static <T> ApiResult<T> error(IResultCode resultCode) {
        ApiResult<T> apiResult = exception(resultCode);
        apiResult.type = ResultTypeEnum.ERROR.getCode();

        return apiResult;
    }

    public static <T> ApiResult<T> error(IResultCode resultCode, String msg) {
        ApiResult<T> apiResult = exception(resultCode, msg);
        apiResult.type = ResultTypeEnum.ERROR.getCode();

        return apiResult;
    }

    public static <T> ApiResult<T> resultWith(IResultCode resultCode, String msg, T data) {
        ApiResult<T> apiResult = new ApiResult<>();
        apiResult.type = ResultTypeEnum.EXCEPTION.getCode();
        apiResult.setCode(resultCode.getCode());
        apiResult.setMsg(msg);
        apiResult.setData(data);

        return apiResult;
    }

    @Transient
    public boolean isSuccess() {
        return this.type == ResultTypeEnum.SUCCESS.getCode();
    }
}
